package com.ymatou.apigateway.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.OpenapiService;
import com.ymatou.apigateway.parameter.xloboAddIdCardBean;
import com.ymatou.apigateway.service.xloboAddIdCardCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.FileUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*身份证上传
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_xloboAddIdCard{
	private static OpenapiReq openapiReq;

	private static xloboAddIdCardBean xloboaddidcardBean;
	private static xloboAddIdCardCall xloboaddidcardCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("身份证上传");
	}

	@Before
	public void caseUp() {
		openapiReq = new OpenapiReq();
		openapiReq.setMethod("xlobo.idcard.upload");

		xloboaddidcardBean=new xloboAddIdCardBean();
		xloboaddidcardCall=new xloboAddIdCardCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(xloboaddidcardCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_xloboAddIdCard_001() {
		Logger.start(true,"身份证上传");
		try {
			//TODO 补全case代码
//			xloboaddidcardBean.setRequest_id(TestDataManager.getRandomNum(12));
//			xloboaddidcardBean.setName("金九");
//			xloboaddidcardBean.setPhone("13916157123");
//			xloboaddidcardBean.setIdcode("411722197209283394");
//			xloboaddidcardBean.setFront_picture(FileUtil.getImageBinary("image/1.jpg"));
//			xloboaddidcardBean.setBack_picture(FileUtil.getImageBinary("image/2.jpg"));
////			xloboaddidcardBean.setBillcode("DB493223849US");
			
			xloboaddidcardBean.setRequest_id(TestDataManager.getRandomNum(12));
			xloboaddidcardBean.setName("彭志邦");
			xloboaddidcardBean.setPhone("15678998765");
			xloboaddidcardBean.setIdcode("430225198901291015");
			xloboaddidcardBean.setFront_picture(FileUtil.getImageBinary("image/1.jpg"));
			xloboaddidcardBean.setBack_picture(FileUtil.getImageBinary("image/2.jpg"));
//			xloboaddidcardBean.setBillcode("DB493223849US");
			
			long sellerId = Long.valueOf(EnvSetup.getData("xlobo_seller"));
			OpenApiWapper openApiWapper = new OpenApiWapper();
			
			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(xloboaddidcardBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
		
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=xlobo.idcard.upload",
							app_id);

			xloboaddidcardCall.setUrl(url);				

			xloboaddidcardCall.setData(openapiReq);
			xloboaddidcardCall.callService();			
			
			Logger.verifyEquals("0000", xloboaddidcardCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, xloboaddidcardCall.getReturnData().contains("{\"upload_result\":1}"), "验证返回内容");
								
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_xloboAddIdCard_002() {
		Logger.start(true,"身份证上传-name空");
		try {
			xloboaddidcardBean.setRequest_id(TestDataManager.getRandomNum(12));
			xloboaddidcardBean.setPhone("15678998765");
			xloboaddidcardBean.setIdcode("430225198901291015");
			xloboaddidcardBean.setFront_picture(FileUtil.getImageBinary("image/1.jpg"));
			xloboaddidcardBean.setBack_picture(FileUtil.getImageBinary("image/2.jpg"));
			
			long sellerId = Long.valueOf(EnvSetup.getData("xlobo_seller"));
			OpenApiWapper openApiWapper = new OpenApiWapper();
			
			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(xloboaddidcardBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
		
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=xlobo.idcard.upload",
							app_id);

			xloboaddidcardCall.setUrl(url);				

			xloboaddidcardCall.setData(openapiReq);
			xloboaddidcardCall.callService();			
			
			Logger.verifyEquals("0001", xloboaddidcardCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, xloboaddidcardCall.getReturnData().contains("name:may not be empty"), "验证返回内容");
								
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_xloboAddIdCard_003() {
		Logger.start(true,"身份证上传-phone空");
		try {
			xloboaddidcardBean.setRequest_id(TestDataManager.getRandomNum(12));
			xloboaddidcardBean.setName("彭志邦");
			xloboaddidcardBean.setIdcode("430225198901291015");
			xloboaddidcardBean.setFront_picture(FileUtil.getImageBinary("image/1.jpg"));
			xloboaddidcardBean.setBack_picture(FileUtil.getImageBinary("image/2.jpg"));
			
			long sellerId = Long.valueOf(EnvSetup.getData("xlobo_seller"));
			OpenApiWapper openApiWapper = new OpenApiWapper();
			
			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(xloboaddidcardBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
		
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=xlobo.idcard.upload",
							app_id);

			xloboaddidcardCall.setUrl(url);				

			xloboaddidcardCall.setData(openapiReq);
			xloboaddidcardCall.callService();			
			
			Logger.verifyEquals("0001", xloboaddidcardCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, xloboaddidcardCall.getReturnData().contains("phone:may not be empty"), "验证返回内容");
									
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_xloboAddIdCard_004() {
		Logger.start(true,"身份证上传-idcode空");
		try {
			xloboaddidcardBean.setRequest_id(TestDataManager.getRandomNum(12));
			xloboaddidcardBean.setName("彭志邦");
			xloboaddidcardBean.setPhone("15678998765");
			xloboaddidcardBean.setFront_picture(FileUtil.getImageBinary("image/1.jpg"));
			xloboaddidcardBean.setBack_picture(FileUtil.getImageBinary("image/2.jpg"));
			
			long sellerId = Long.valueOf(EnvSetup.getData("xlobo_seller"));
			OpenApiWapper openApiWapper = new OpenApiWapper();
			
			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(xloboaddidcardBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
		
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=xlobo.idcard.upload",
							app_id);

			xloboaddidcardCall.setUrl(url);				

			xloboaddidcardCall.setData(openapiReq);
			xloboaddidcardCall.callService();			
			
			Logger.verifyEquals("0001", xloboaddidcardCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, xloboaddidcardCall.getReturnData().contains("idcode:may not be empty"), "验证返回内容");
									
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_xloboAddIdCard_005() {
		Logger.start(true,"身份证上传-front_picture空");
		try {
			xloboaddidcardBean.setRequest_id(TestDataManager.getRandomNum(12));
			xloboaddidcardBean.setName("彭志邦");
			xloboaddidcardBean.setPhone("15678998765");
			xloboaddidcardBean.setIdcode("430225198901291015");
			xloboaddidcardBean.setBack_picture(FileUtil.getImageBinary("image/2.jpg"));
//			xloboaddidcardBean.setBillcode("DB493223849US");
			
			long sellerId = Long.valueOf(EnvSetup.getData("xlobo_seller"));
			OpenApiWapper openApiWapper = new OpenApiWapper();
			
			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(xloboaddidcardBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
		
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=xlobo.idcard.upload",
							app_id);

			xloboaddidcardCall.setUrl(url);				

			xloboaddidcardCall.setData(openapiReq);
			xloboaddidcardCall.callService();			
			
			Logger.verifyEquals("0001", xloboaddidcardCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, xloboaddidcardCall.getReturnData().contains("frontPicture:may not be empty"), "验证返回内容");
									
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_xloboAddIdCard_006() {
		Logger.start(true,"身份证上传-back_picture空");
		try {
			xloboaddidcardBean.setRequest_id(TestDataManager.getRandomNum(12));
			xloboaddidcardBean.setName("彭志邦");
			xloboaddidcardBean.setPhone("15678998765");
			xloboaddidcardBean.setIdcode("430225198901291015");
			xloboaddidcardBean.setFront_picture(FileUtil.getImageBinary("image/1.jpg"));
//			xloboaddidcardBean.setBillcode("DB493223849US");
			
			long sellerId = Long.valueOf(EnvSetup.getData("xlobo_seller"));
			OpenApiWapper openApiWapper = new OpenApiWapper();
			
			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(xloboaddidcardBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
		
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=xlobo.idcard.upload",
							app_id);

			xloboaddidcardCall.setUrl(url);				

			xloboaddidcardCall.setData(openapiReq);
			xloboaddidcardCall.callService();			
			
			Logger.verifyEquals("0001", xloboaddidcardCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, xloboaddidcardCall.getReturnData().contains("backPicture:may not be empty"), "验证返回内容");
								
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_xloboAddIdCard_007() {
		Logger.start(true,"身份证上传-带面单");
		try {
			//TODO 补全case代码
			xloboaddidcardBean.setRequest_id(TestDataManager.getRandomNum(12));
			xloboaddidcardBean.setName("金九");
			xloboaddidcardBean.setPhone("13916157123");
			xloboaddidcardBean.setIdcode("361102198602261337");
			xloboaddidcardBean.setFront_picture(FileUtil.getImageBinary("image/1.jpg"));
			xloboaddidcardBean.setBack_picture(FileUtil.getImageBinary("image/2.jpg"));
			xloboaddidcardBean.setBillcode("DB493223849US");
			
			long sellerId = Long.valueOf(EnvSetup.getData("xlobo_seller"));
			OpenApiWapper openApiWapper = new OpenApiWapper();
			
			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(xloboaddidcardBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
		
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=xlobo.idcard.upload",
							app_id);

			xloboaddidcardCall.setUrl(url);				

			xloboaddidcardCall.setData(openapiReq);
			xloboaddidcardCall.callService();			
			
			Logger.verifyEquals("0000", xloboaddidcardCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, xloboaddidcardCall.getReturnData().contains("{\"upload_result\":0}"), "验证返回内容");
								
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}